package com.rachel.utilslib.viewpager;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 该类为 viewpager 的工具类，用于导航栏、首页轮播加载或者试图切换等作用
 * Created by Administrator on 2017/10/19.
 */

public class ViewPagerHelper {
    private static final String TAG = "zsr";
    /**
     * ui
     */
    private ViewPager mViewPager;


    /**
     * others
     */
    private Context mContext;
    private boolean isGlide = false;


    public ViewPagerHelper(Builder builder){
        mViewPager = builder.viewPager;
        mContext = builder.context;
        // 首次引导图
        if (builder.isGlide) {
            GlidePagerAdapter adapter = new GlidePagerAdapter(builder.views);
            mViewPager.setAdapter(adapter);
            showGlideMode(builder);
        }

        // 轮播控件
        if (builder.isAuto){
            AutoPagerAdapter adapter = new AutoPagerAdapter(builder.views);
            mViewPager.setAdapter(adapter);
        }

    }

    private void showGlideMode(final Builder builder) {
        isGlide = builder.isGlide;
        if (builder.isGlide){
            // imageviews 用来保存底部 圆点，用于切换状态
            final List<ImageView> imageViews = new ArrayList<>();
            final LinearLayout bottomLayout = builder.linearLayout;
            /**
             * 用来填充底部小圆点，或者其他东西,由于在这里已经对 imageview 的整体大小定下了，如果在 selector 中
             * 使用不同大小的圆点，这里在每次切换的page的时候，要再配置一下大小，为了保持代码的可读性，这里暂时就不
             * 添加了，有需要可以试试，或者我后期添加进去
             */

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(15,0,0,0); //每个点的间距
            for (int i = 0; i < builder.views.size(); i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setImageResource(builder.selector);
                if (i == 0){
                    imageView.setSelected(true); //默认第一个为true
                }else{
                    imageView.setSelected(false);
                }
                imageView.setLayoutParams(params);
                if (bottomLayout != null) {
                    bottomLayout.addView(imageView);
                }
                imageViews.add(imageView);
            }
            // glide mode 下的viewpager 翻页事件
            final View startBtn = builder.startView;
            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    for (int i = 0; i < imageViews.size(); i++) {
                        ImageView imageView = imageViews.get(i);
                        if (i == position){
                            imageView.setSelected(true);
                        }else{
                            imageView.setSelected(false);
                        }
                    }
                    // 最后一页
                    if (position == imageViews.size() - 1){
                        if (startBtn != null){
                            startBtn.setVisibility(View.VISIBLE);
                            if (bottomLayout != null) {
                                bottomLayout.setVisibility(View.GONE);
                            }
                            ObjectAnimator animator = ObjectAnimator.ofFloat(startBtn,
                                    "alpha",0,1);
                            animator.setDuration(500);
                            animator.setInterpolator(new AccelerateInterpolator());
                            animator.start();
                        }
                    }else{
                        if (startBtn != null){
                            startBtn.setVisibility(View.GONE);
                            if (bottomLayout != null) {
                                bottomLayout.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }




    public static class Builder{
        Context context;
        ViewPager viewPager;
        LinearLayout linearLayout;
        List<View> views;
        int selector;
        boolean isGlide;
        View startView;
        boolean isAuto;
        long autoTime;

        public Builder setContext(Context context){
            this.context = context;
            return this;
        }
        public Builder setViewPager(ViewPager viewPager){
            this.viewPager = viewPager;
            return this;
        }
        public Builder setLinearLayout(LinearLayout linearLayout){
            this.linearLayout = linearLayout;
            return this;
        }

        public Builder setStartView(View view){
            this.startView = view;
            return  this;
        }


        public Builder setDataViews(List<View> views){
            this.views = views;
            return this;
        }
        public Builder setSelector(int selector){
            this.selector = selector;
            return this;
        }
        public Builder setIsGlide(boolean isGlide){
            this.isGlide = isGlide;
            return this;
        }


        public Builder setIsAuto(boolean isAuto){
            this.isAuto = isAuto;
            return this;
        }

        public Builder setAutoTime(long autoTime){
            this.autoTime = autoTime;
            return this;
        }

        public ViewPagerHelper builder(){
            return new ViewPagerHelper(this);
        }
    }

}
